<?php

function scot_generate($model) {
	scot_user_list($model);
	scot_tag_list($model);
	scot_cooccurrence_list($model);
	
	return scot_serialize($model);
}

function scot_user_list(&$model) {
	$user_list = array();
	
	foreach($model['tagging_activity_list'] as $tagging_activity) {
		if(!in_array($tagging_activity['user'], $user_list)) {
			$user_list[] = $tagging_activity['user'];
		}
	}

	$model['user_list'] = $user_list;
}

function scot_tag_list(&$model) {
	$tag_list = array();
	
	$tatal_tag_freq = 0;
	
	foreach($model['tagging_activity_list'] as $id => $tagging_activity) {
		foreach($tagging_activity['tags'] as $tag) {
			$h = hash("md5", $tag);
			if(key_exists($h, $tag_list)) {
				$tag_list[$h]['af']++;
				$tag_list[$h]['taggings'][] = $id;
			} else {
				$tag_list[$h] = array('name'=>$tag, 'af'=>1, 'cos' => array(), 'taggings'=> array($id));
			}
			$tatal_tag_freq++;
		}
	}
	foreach($tag_list as $h => $tag) {
		$tag_list[$h]['rf'] = sprintf("%1\$.2f", $tag['af'] / $tatal_tag_freq * 100);
	}

	$model['tag_list'] = $tag_list;
}

function scot_cooccurrence_list(&$model) {
	$cooccurrence_list = array();

	$tatal_cooccurrence_freq = 0;
	
	foreach($model['tagging_activity_list'] as $tagging_activity) {
		if(count($tagging_activity['tags']) < 2) continue;

		$tags = '';
		$co_tag_list = array();
		foreach($tagging_activity['tags'] as $tag) {
			$tags.= $tag.' ';
			$h = hash("md5", $tag);
			$co_tag_list[] = $h;
		}
		
		$h = hash("md5", $tags);
		
		if(key_exists($h, $cooccurrence_list)) {
			$cooccurrence_list[$h]['af']++;
		} else {
			$cooccurrence_list[$h] = array('tags'=>$co_tag_list, 'af'=>1);
			foreach($co_tag_list as $t) {
				$model['tag_list'][$t]['cos'][] = $h;
			}
		}
		$tatal_cooccurrence_freq++;
	}
	foreach($cooccurrence_list as $h => $co) {
		$cooccurrence_list[$h]['rf'] = sprintf("%1\$.2f", $co['af'] / $tatal_cooccurrence_freq * 100);
	}
	
	$model['cooccurrence_list'] = $cooccurrence_list;
}

function scot_serialize($model) {
	$rdf = '<?xml version="1.0" encoding="UTF-8" ?>'."\n";
	$rdf .='<!-- Generated by the SCOT Exporter'."\n";
	$rdf .='# http://scot-project.org !-->'."\n";
	$rdf .= '<rdf:RDF'."\n";
	$rdf .= 'xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#" '."\n";
	$rdf .= 'xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" '."\n";
	$rdf .= 'xmlns:dc="http://purl.org/dc/elements/1.1/" '."\n";
	$rdf .= 'xmlns:dcterms="http://purl.org/dc/terms/" '."\n";
	$rdf .= 'xmlns:foaf="http://xmlns.com/foaf/0.1/" '."\n";
	$rdf .= 'xmlns:skos="http://www.w3.org/2004/02/skos/core#" '."\n";
	$rdf .= 'xmlns:sioc="http://rdfs.org/sioc/ns#" '."\n";
	$rdf .= 'xmlns:tags="http://www.holygoat.co.uk/owl/readwood/0.1/tags/" '."\n";
	$rdf .= 'xmlns:scot="http://scot-project.org/scot/ns#">'."\n";
	
	$uri = $model['meta']['base_uri'];
	$tagcloud_uri = $uri.'tagcloud';
	$tag_uri = $uri.'tag/';
	$co_uri = $uri.'cooccerrence/';
	$tagging_uri = $uri.'tagging/';
	$user_uri = $uri.'user/';
	
	$rdf .= "\n	<!-- Tagcloud Start !-->\n";
	$rdf .= '	<scot:Tagcloud rdf:about="'.$tagcloud_uri.'">'."\n";
	
	$rdf .= "\n		<!-- Tagcloud Metadata !-->\n";
	$rdf .=	'		<dc:creator rdf:resource="'.$model['meta']['creator_uri'].'"/>'."\n";
	$rdf .=	'		<dc:publisher rdf:resource="'.$model['meta']['publisher_uri'].'"/>'."\n";
	$rdf .=	'		<dcterms:created>'.date('Y-m-d',time()).'T'.date('H:i:s',time()).'</dcterms:created>'."\n";
	
	$rdf .=	'		<scot:totalPosts>'.count($model['tagging_activity_list']).'</scot:totalPosts>'."\n";
	$rdf .=	'		<scot:totalTags>'.count($model['tag_list']).'</scot:totalTags>'."\n";
	$rdf .=	'		<scot:totalCooccurrences>'.count($model['cooccurrence_list']).'</scot:totalCooccurrences>'."\n";

	$cnt = 0;
	foreach($model['tag_list'] as $t) $cnt += $t['af'];
	$rdf .=	'		<scot:totalTagFrequency>'.$cnt.'</scot:totalTagFrequency>'."\n";
	$cnt = 0;
	foreach($model['cooccurrence_list'] as $c) $cnt += $c['af'];
	$rdf .=	'		<scot:totalCooccurFrequency>'.$cnt.'</scot:totalCooccurFrequency>'."\n";
	
	
	$rdf .= "\n		<!-- Tagcloud Usergroup !-->\n";
	$rdf .=	'		<scot:hasUsergroup>'."\n";
	$rdf .=	'			<sioc:Usergroup>'."\n";
	foreach($model['user_list'] as $user) {
		$rdf .=	'				<sioc:has_member>'."\n";
		$rdf .=	'					<sioc:User rdf:about="'.$user_uri.urlencode($user).'">'."\n";
		$rdf .=	'						<sioc:name>'.$user.'</sioc:name>'."\n";
		$rdf .=	'					</sioc:User>'."\n";
		$rdf .=	'				</sioc:has_member>'."\n";
	}
	$rdf .=	'			</sioc:Usergroup>'."\n";
	$rdf .=	'		</scot:hasUsergroup>'."\n";
	

	
	$rdf .= "\n		<!-- Tagcloud Tags !-->\n";
	foreach($model['tag_list'] as $h => $tag) {
		$rdf .= "		<scot:contains>\n";
		$rdf .= "			<scot:Tag rdf:about=\"".$tag_uri.urlencode($tag['name'])."\">\n";
		$rdf .= "				<scot:name>".$tag['name']."</scot:name>\n";
		$rdf .= "				<scot:ownAFrequency>".$tag['af']."</scot:ownAFrequency>\n";
		$rdf .= "				<scot:ownRFrequency>".$tag['rf']."</scot:ownRFrequency>\n";
		
		foreach($tag['cos'] as $co) {
			$rdf .= '				<scot:cooccursIn rdf:resource="'.$co_uri.$co.'"/>'."\n";
		}

		foreach($tag['taggings'] as $tagging_id) {
			$tagging_tag_uri = $tagging_uri.$tagging_id.'/tag/'.urlencode($tag['name']);
			$rdf .= '				<scot:aggregatesTag rdf:resource="'.$tagging_tag_uri.'"/>'."\n";
		}
//
//		foreach($tag['taggings'] as $tagging_id) {
//			$rdf .= '				<scot:tagOf rdf:resource="'.$model['tagging_activity_list'][$tagging_id]['item']['link'].'"/>'."\n";
//		}
//
//		foreach($tag['taggings'] as $tagging_id) {
//			$rdf .= '				<scot:usedBy rdf:resource="'.$user_uri.urlencode($model['tagging_activity_list'][$tagging_id]['user']).'"/>'."\n";
//		}
		
		$rdf .= '			</scot:Tag>'."\n";
		$rdf .= '		</scot:contains>'."\n";
	}	

	$rdf .= "\n		<!-- Tagcloud Tagging Activities !-->\n";
	foreach($model['tagging_activity_list'] as $id => $tagging) {
		if(count($tagging['tags']) == 0) continue;
		$rdf .= '		<scot:taggingActivity>'."\n";
		$rdf .= '			<tags:Tagging rdf:about="'.$tagging_uri.$id.'">'."\n";
		$rdf .= '				<tags:taggedBy rdf:resource="'.$user_uri.urlencode($tagging['user']).'"/>'."\n";
		$rdf .= '				<scot:taggedItem rdf:resource="'.$tagging['item']['link'].'"/>'."\n";
		$tagging_tag_uri = $tagging_uri.$id.'/tag/';
		foreach($tagging['tags'] as $tag) {
			$rdf .= '				<scot:tagUsed rdf:resource="'.$tagging_tag_uri.urlencode($tag).'"/>'."\n";
		}
		$rdf .= '			</tags:Tagging>'."\n";
		$rdf .= '		</scot:taggingActivity>'."\n";
	}		
	
	$rdf .= '	</scot:Tagcloud>'."\n";
	
	
	$rdf .= "\n	<!-- Tagcloud Cooccurrences !-->\n";
	foreach($model['cooccurrence_list'] as $h => $co) {
		$rdf .= '	<scot:Cooccurrence rdf:about="'.$co_uri.$h.'">'."\n";
		$rdf .= '		<scot:cooccurAFrequency>'.$co['af'].'</scot:cooccurAFrequency>'."\n";
		$rdf .= '		<scot:cooccurRFrequency>'.$co['rf'].'</scot:cooccurRFrequency>'."\n";
		
		foreach($co['tags'] as $tag) {
			$rdf .= '		<scot:cooccurTag rdf:resource="'.$tag_uri.urlencode($model['tag_list'][$tag]['name']).'"/>'."\n";
		}
		
		$rdf .= '	</scot:Cooccurrence>'."\n";
	}	
	
	$rdf .= '</rdf:RDF>'."\n";
	return $rdf;
}

?>